package com.steak.concurrent.sleep;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LockSleepTest {
    private static final Lock lock = new ReentrantLock();
    public static void main(String[] args) {
        new Thread(() -> {
            lock.lock();
            try {
                System.out.println(Thread.currentThread().getName()+"  sleep1");
                Thread.sleep(2000);
                System.out.println(Thread.currentThread().getName()+"  awake1");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        }).start();

        new Thread(() -> {
            lock.lock();
            try {
                System.out.println(Thread.currentThread().getName()+"  sleep2");
                Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName()+"  awake2");
            }catch (InterruptedException e){
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        }).start();
    }
}
